# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import logging
import sys

from rest_framework.decorators import api_view

sys.path.extend(["F:/ML/BNC", "D:/BNC", "/www/wwwroot/BNC"])

import demjson
from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from django.utils import timezone
from datetime import datetime as _dt

from .Utils.Fnc import *
from .exceptions.exceptions import IllegalArgsException
from .service import market_temperature, market_high_limit, stock_calendar as stock_calendar_m, tag_rela_code, \
    tag_rela_code_by_date_tag_plate, continue_stock_info, code_rela_tag

log = logging.getLogger(__name__)
DATE_FORMAT = "%Y%m%d"
cur_timezone = timezone.get_current_timezone()


@csrf_exempt
@require_http_methods(["GET", "POST"])
def index(request):
    return render(request, "index.html")


@csrf_exempt
@require_http_methods(["GET", "POST"])
def more_json(request):
    with open("F:/ML/django/blog/files/MarketThermometer.json", mode='r', encoding='utf8') as f:
        jso_s = f.read()
    return JsonResponse(demjson.decode(jso_s), safe=False)


@csrf_exempt
@require_http_methods(["GET", "POST"])
def dayup(request):
    return render(request, 'MT.html')


@csrf_exempt
@require_http_methods(["GET", "POST"])
def tag_edit(request):
    return render(request, 'ReplayData.html')


@csrf_exempt
@require_http_methods(["GET", "POST"])
def market_temperature_a(request):
    res = {}
    # 获取相应的参数 如何获取到数据类型
    fail = True
    try:
        start_date = None
        end_date = None
        size = None
        if "datetimeStart" in request.POST:
            start_date = request.POST.get("datetimeStart")
        if "datetimeEnd" in request.POST:
            end_date = request.POST.get("datetimeEnd")
        if "size" in request.POST:
            size = request.POST.get("size")
        # 开启默认值
        # 修改最近的时间
        # start_date = 20191209

        pre_json = market_temperature(start_date=start_date, end_date=end_date, size=size)
        res = pre_json
        # return JsonResponse(data=pre_json, safe=True)
    except IllegalArgsException as iae:
        log.exception(iae)
        res = generate_message_dict_4_fail(500, '时间范围错误')
    except Exception as e:
        # 不可预知的错误
        log.exception(e)
        res = generate_message_dict_4_fail(500, e.args)
    else:
        fail = False
    finally:
        # 最终提供json的返回
        res = wrapper_res_dict(web_res=res, fail=fail)
        return JsonResponse(data=res, safe=True)


@csrf_exempt
@require_http_methods(["GET", "POST"])
def high_limit_continuous_a(request):
    res = {}
    # 获取相应的参数 如何获取到数据类型
    fail = True
    try:
        start_date = None
        end_date = None
        size = None
        if "datetimeStart" in request.POST:
            start_date = request.POST.get("datetimeStart")
        if "datetimeEnd" in request.POST:
            end_date = request.POST.get("datetimeEnd")
        if "size" in request.POST:
            size = request.POST.get("size")
        pre_json = market_high_limit(start_date=start_date, end_date=end_date, size=size)
        res = pre_json
        # return JsonResponse(data=pre_json, safe=True)
    except IllegalArgsException as iae:
        log.exception(iae)
        res = generate_message_dict_4_fail(500, '时间范围错误')
    except Exception as e:
        # 不可预知的错误
        log.exception(e)
        res = generate_message_dict_4_fail(500, e.args)
    else:
        fail = False
    finally:
        # 最终提供json的返回
        res = wrapper_res_dict(web_res=res, fail=fail)
        return JsonResponse(data=res, safe=True)


@csrf_exempt
@require_http_methods(["GET", "POST"])
def stock_calendar_a(request):
    """ 返回股票日历 """
    res = {}
    # 获取相应的参数 如何获取到数据类型
    fail = True
    try:
        start_date = None
        end_date = None
        size = None
        if "datetimeStart" in request.POST:
            start_date = request.POST.get("datetimeStart")
        if "datetimeEnd" in request.POST:
            end_date = request.POST.get("datetimeEnd")
        if "size" in request.POST:
            size = request.POST.get("size")
        pre_json = stock_calendar_m(start_date=start_date, end_date=end_date, size=size)
        res = pre_json
        # return JsonResponse(data=pre_json, safe=True)
    except IllegalArgsException as iae:
        log.exception(iae)
        res = generate_message_dict_4_fail(500, '时间范围错误')
    except Exception as e:
        # 不可预知的错误
        log.exception(e)
        res = generate_message_dict_4_fail(500, e.args)
    else:
        fail = False
    finally:
        # 最终提供json的返回
        res = wrapper_res_dict(web_res=res, fail=fail)
        return JsonResponse(data=res, safe=True)


@csrf_exempt
@require_http_methods(["GET", "POST"])
def rela_codes_by_d_t_p(request):
    """ 返回股票日历 """
    res = {}
    # 获取相应的参数 如何获取到数据类型
    fail = True
    try:
        date = None
        tag = None
        plate = None
        if "date" in request.POST:
            date = request.POST.get("date")
        if "tag" in request.POST:
            tag = request.POST.get("tag")
        if "plate" in request.POST:
            plate = request.POST.get("plate")

        pre_json = tag_rela_code_by_date_tag_plate(date, tag, plate)
        res = pre_json
        # return JsonResponse(data=pre_json, safe=True)
    except Exception as e:
        # 不可预知的错误
        log.exception(e)
        res = generate_message_dict_4_fail(500, e.args)
    else:
        fail = False
    finally:
        # 最终提供json的返回
        res = wrapper_res_dict(web_res=res, fail=fail)
        return JsonResponse(data=res, safe=True)


@csrf_exempt
# @require_http_methods(["GET", "POST"])
@api_view(['POST'])
def tag_rela_codes(request):
    """ 返回股票日历 """
    res = {}
    # 获取相应的参数 如何获取到数据类型
    fail = True
    try:
        tag = request.data.get("tag",None)
        num = request.data.get("num", None)
        pre_json = tag_rela_code(tag=tag, num=num)
        res = pre_json
        # return JsonResponse(data=pre_json, safe=True)
    except Exception as e:
        # 不可预知的错误
        log.exception(e)
        res = generate_message_dict_4_fail(500, e.args)
    else:
        fail = False
    finally:
        # 最终提供json的返回
        res = wrapper_res_dict(web_res=res, fail=fail)
        return JsonResponse(data=res, safe=True)

@csrf_exempt
@api_view(['POST'])
def code_rela_tags(request):
    """ 返回股票日历 """
    res = {}
    # 获取相应的参数 如何获取到数据类型
    fail = True
    try:
        code = request.data.get("code",None)
        pre_json = code_rela_tag(code_ = code)
        res = pre_json
        # return JsonResponse(data=pre_json, safe=True)
    except Exception as e:
        # 不可预知的错误
        log.exception(e)
        res = generate_message_dict_4_fail(500, e.args)
    else:
        fail = False
    finally:
        # 最终提供json的返回
        res = wrapper_res_dict(web_res=res, fail=fail)
        return JsonResponse(data=res, safe=True)


@csrf_exempt
@require_http_methods(["GET", "POST"])
def continue_stock_info_a(request):
    """ 返回股票日历 """
    res = {}
    # 获取相应的参数 如何获取到数据类型
    fail = True
    try:
        cur_date = None
        cur_tag = None
        __POST = request.POST
        if "cur_date" in __POST:
            cur_date = __POST.get("cur_date")
        if "cur_tag" in __POST:
            cur_tag = __POST.get("cur_tag")

        pre_json = continue_stock_info(cur_date, cur_tag)
        res = pre_json
        # return JsonResponse(data=pre_json, safe=True)
    except Exception as e:
        # 不可预知的错误
        log.exception(e)
        res = generate_message_dict_4_fail(500, e.args)
    else:
        fail = False
    finally:
        # 最终提供json的返回
        res = wrapper_res_dict(web_res=res, fail=fail)
        return JsonResponse(data=res, safe=True)


@csrf_exempt
@require_http_methods(['GET'])
def market_temperature_html(request):
    return render(request, 'MarketTemperature.html')


@csrf_exempt
@require_http_methods(['GET'])
def market_thermometer_html(request):
    return render(request, 'MarketThermometer.html')


@csrf_exempt
@require_http_methods(['GET'])
def stock_calendar_html(request):
    """ 股票日历的html """
    return render(request, 'StockCalendar.html')

